package com.i.template.constant;

import cn.hutool.core.util.BooleanUtil;
import lombok.Builder;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * @author lewis
 * @since 24/07/24
 * @type class
 * @description controller层 返回结果集
 */
@Data
@Accessors(chain = true)
//@Builder(toBuilder = true)
public class ResponseResult<T> {
    private Integer code;
    private T data;
    private String message;

    public ResponseResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResponseResult(Integer code, T data, String message) {
        this(code, message);
        this.data = data;
    }
    public static ResponseResult<Boolean> judge(Boolean flag){
        if(BooleanUtil.isTrue(flag)) {
            return ResponseResult.ok(true);
        }else {
            return ResponseResult.fail();
        }
    }
    public static <T> ResponseResult<T> ok(T data) {
        return new ResponseResult<>(ResponseStatusEnum.OK.getCode(), data, ResponseStatusEnum.OK.getMessage());
    }
    public static <T> ResponseResult<T> ok(T data,String message) {
        return new ResponseResult<>(ResponseStatusEnum.OK.getCode(), data, message);
    }
    public static <T> ResponseResult<T> fail(){
        return new ResponseResult<>(ResponseStatusEnum.FAIL.getCode(),ResponseStatusEnum.FAIL.getMessage());
    }
    public static <T> ResponseResult<T> fail(String message){
        return new ResponseResult<>(ResponseStatusEnum.FAIL.getCode(),message);
    }
}
